package com.lazily.map_;


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @Author: GZQ
 * @date: 2025/6/2 16:46
 * @description: Map接口练习
 * @version: 1.0
 */


@SuppressWarnings({"all"})
public class MapExercise {
	// 程序入口
	public static void main(String[] args) {
		// 完成代码
		Map hashMap = new HashMap();
		hashMap.put(1, new Employee(1, "jack", 30000));
		hashMap.put(2, new Employee(2, "tom", 21000));
		hashMap.put(3, new Employee(3, "mary", 18000));


		// 遍历 2 种方式
		// 并遍历显示工资>18000 的员工(遍历方式最少两种)
		// 1. 使用keySet --> 增强for
		Set keySet = hashMap.keySet();
		System.out.println("--------第一种遍历方式--------");
		for (Object key : keySet) {
			// 	先获取value
			Employee employee = (Employee)hashMap.get(key);
			if (employee.getSalary() > 18000) {
				System.out.println(employee);
			}
		}

		// 2. 使用EntrySet --> 迭代器
		// 	  体现比较难的知识点
		Set entrySet = hashMap.entrySet();
		System.out.println("--------第二种遍历方式--------");
		Iterator iterator = entrySet.iterator();
		while (iterator.hasNext()) {
			Map.Entry entry = (Map.Entry)iterator.next();
			System.out.println(entry.getClass()); // HashMap$Node
			// 	通过entry 取得key 和 value
			Employee employee = (Employee)entry.getValue();
			if (employee.getSalary() > 18000) {
				System.out.println(employee);
			}
		}

	}

}


/**
 * 使用 HashMap 添加 3 个员工对象，要求
 * 键：员工 id
 * 值：员工对象
 * <p>
 * 并遍历显示工资>18000 的员工(遍历方式最少两种)
 * 员工类：姓名、工资、员工 id
 */

@SuppressWarnings({"all"})
class Employee {
	int id;
	String name;
	double salary;

	public Employee(int id, String name, double salary) {
		this.id = id;
		this.name = name;
		this.salary = salary;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	@Override
	public String toString() {
		return "Employee{" +
				"id=" + id +
				", name='" + name + '\'' +
				", salary=" + salary +
				'}';
	}
}